package cn.wolfcode.wms.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.wolfcode.wms.domain.ProductStock;
import cn.wolfcode.wms.domain.StockIncomeBill;
import cn.wolfcode.wms.domain.StockIncomeBillItem;
import cn.wolfcode.wms.mapper.ProductStockMapper;
import cn.wolfcode.wms.mapper.StockIncomeBillItemMapper;
import cn.wolfcode.wms.mapper.StockIncomeBillMapper;
import cn.wolfcode.wms.query.PageResult;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IProductStockService;
import cn.wolfcode.wms.service.IStockIncomeBillService;
import cn.wolfcode.wms.util.UserContext;

@Service
public class StockIncomeBillServiceImpl implements IStockIncomeBillService{

	@Autowired
	private StockIncomeBillMapper mapper;
	@Autowired
	private StockIncomeBillItemMapper itemMapper;
	@Autowired
	private ProductStockMapper productStockMapper;
	
	
	@Override
	public void insert(StockIncomeBill stockBill) {
		//录入时间
		stockBill.setInputTime(new Date());
		//录入人
		stockBill.setInputUser(UserContext.getUser());
		
		List<StockIncomeBillItem> items = stockBill.getItems();
		//总数量
		BigDecimal totalNumber = BigDecimal.ZERO;
		//总金额
		BigDecimal totalAmount = BigDecimal.ZERO;
		for (StockIncomeBillItem item : items) {
			//小计
			item.setAmount(item.getCostPrice().multiply(item.getNumber()));
			totalNumber=totalNumber.add(item.getNumber());
			totalAmount=totalAmount.add(item.getAmount());
		}
		stockBill.setTotalNumber(totalNumber);
		stockBill.setTotalAmount(totalAmount);
		mapper.insert(stockBill);
		
		//订单id
		for (StockIncomeBillItem item : items) {
			item.setBillId(stockBill.getId());
			//保存明细
			itemMapper.insert(item);
		}
	}

	@Override
	public void deleteByPrimaryKey(Long id) {
		mapper.deleteByPrimaryKey(id);
		itemMapper.deleteByBillId(id);
	}

	@Override
	public void updateByPrimaryKey(StockIncomeBill stockBill) {
		//删除旧的明细
		itemMapper.deleteByBillId(stockBill.getId());
		//修改订单,总数,总额,
		List<StockIncomeBillItem> items = stockBill.getItems();
		//总数量
		BigDecimal totalNumber = BigDecimal.ZERO;
		//总金额
		BigDecimal totalAmount = BigDecimal.ZERO;
		for (StockIncomeBillItem item : items) {
			//小计
			item.setAmount(item.getCostPrice().multiply(item.getNumber()));
			totalNumber=totalNumber.add(item.getNumber());
			totalAmount=totalAmount.add(item.getAmount());
			//保存明细
			item.setBillId(stockBill.getId());
			itemMapper.insert(item);
		}
		stockBill.setTotalNumber(totalNumber);
		stockBill.setTotalAmount(totalAmount);
		
		mapper.updateByPrimaryKey(stockBill);
	}

	@Override
	public StockIncomeBill selectByPrimaryKey(Long id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<StockIncomeBill> selectAll() {
		return mapper.selectAll();
	}

	@Override
	public PageResult query(QueryObject qo) {
		int totalCount = mapper.queryForCount(qo);
		if (totalCount == 0) {
			return new PageResult(qo.getPageSize());
		}
		List<StockIncomeBill> listData = mapper.queryForList(qo);
		return new PageResult(qo.getCurrentPage(), qo.getPageSize(), listData, totalCount);
	}

	@Override
	public void audit(Long id) {
		//根据id查询订单
		StockIncomeBill stockBill = mapper.selectByPrimaryKey(id);
		//判断该订单是否已审核
		if (stockBill.getStatus()==StockIncomeBill.STATUS_NORMAL) {
			//设置状态
			stockBill.setStatus(StockIncomeBill.STATUS_AUDITED);
			//设置审核人
			stockBill.setAuditor(UserContext.getUser());
			//设置审核时间
			stockBill.setAuditTime(new Date());
			//审核订单 更新数据
			mapper.audit(stockBill);
			
			//更新库存
			//根据商品id和仓库id 查询商品库存数据
			//遍历
			Long depotId = stockBill.getDepot().getId();
			for (StockIncomeBillItem item : stockBill.getItems()) {
				ProductStock stock = productStockMapper.selectByProductIdAndDepotId(item.getProduct().getId(),depotId);
				//判断库存中是否有该商品
				if (stock!=null) {
					//修改总额
					BigDecimal Amount = stock.getAmount().add(item.getAmount());
					stock.setAmount(Amount);
					//修改总数
					BigDecimal storeNumber = stock.getStoreNumber().add(item.getNumber());
					stock.setStoreNumber(storeNumber);
					//修改单价
					stock.setPrice(Amount.divide(storeNumber, 2, BigDecimal.ROUND_HALF_UP));
					//更新数据
					productStockMapper.updateByPrimaryKey(stock);
				}else {
					//直接保存
					ProductStock newStock = new ProductStock();
					newStock.setPrice(item.getCostPrice());
					newStock.setStoreNumber(item.getNumber());
					newStock.setAmount(item.getAmount());
					newStock.setProduct(item.getProduct());
					newStock.setDepot(stockBill.getDepot());
					productStockMapper.insert(newStock);
				}
			}
		}
	}
}
